package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.Result;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.SetmealDish;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);

        return Result.success("新增套餐成功！！");
    }


    //    分页查询
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name) {
//        构造分页对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> pageInfoDto = new Page<>(page, pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
//        添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
//        添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
//        进行分页查询
        setmealService.page(pageInfo, queryWrapper);
//        对象拷贝
        BeanUtils.copyProperties(pageInfo, pageInfoDto, "records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);

            setmealDto.setCategoryName(category.getName());

            return setmealDto;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(list);

        return Result.success(pageInfoDto);

    }

    //删除套餐
    @DeleteMapping
    public Result<String> delete(String ids) {
//            切割字符串
        String[] split = ids.split(",");

        for (String s : split) {
            Long id = Long.valueOf(s);
            setmealService.removeById(id);
            setmealDishService.removeBySetmealId(id);
        }

        return Result.success("删除套餐成功！！");
    }

    //获取套餐详细
    @GetMapping("/{id}")
    public Result<SetmealDto> get(@PathVariable long id) {
        SetmealDto setmealDto = setmealService.getIdWithDish(id);

        return Result.success(setmealDto);
    }

    //    修改套餐详情
    @PutMapping
    public Result<String> update(@RequestBody SetmealDto setmealDto) {

        setmealService.updateWithDish(setmealDto);

        return Result.success("更新成功！");
    }


    //    修改套餐状态
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, String ids) {

        String[] idArr = ids.split(",");

        for (String s : idArr) {
            Long id = Long.valueOf(s);
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }

        return Result.success("修改成功！");
    }
}
